<?php

namespace app\distribution\controller\sys_admin;

use app\mainadmin\model\LadderModel;
use app\member\model\UsersLevelApplyModel;
use app\shop\model\GoodsModel;
use think\Db;

use app\AdminController;

use app\distribution\model\DividendModel;

/**
 * 分销设置
 * Class Index
 * @package app\store\controller
 */
class Ladder extends AdminController
{
    /*------------------------------------------------------ */
    //-- 优先执行
    /*------------------------------------------------------ */
    public function initialize()
    {
        parent::initialize();
        $this->Model = new LadderModel();
    }
    /*------------------------------------------------------ */
    //-- 主页
    /*------------------------------------------------------ */
    public function index()
    {
//        $row = $this->Model->getRows(1);
        $setting = Db::name('lc_config')->where('id',1)->find();
        if($setting['team']){
            $setting['team'] = json_decode($setting['team'],true);
        }
        if($setting['star']){
            $setting['star'] = json_decode($setting['star'],true);
        }
        $this->assign('setting', $setting);
        return $this->fetch();
    }

    /*------------------------------------------------------ */
    //-- 保存配置
    /*------------------------------------------------------ */
    public function save()
    {
        $post = input();
        $data = [
            'open_team' => $post['open_team'],
            'open_star' => $post['open_star'],
            'team' => [
                'goods' => [],
                'config' => []
            ],
            'star' => [
                'goods' => [],
                'config' => []
            ]
        ];
        if($post['goods_id']){
            foreach ($post['goods_id'] as $k => $val){
                $data['team']['goods'][] = [
                    'goods_id' => $val,
                    'goods_img' => $post['goods_img'][$k]
                ];
            }
        }
        if($post['min_order_num']){
            foreach ($post['min_order_num'] as $k => $val){
                $data['team']['config'][] = [
                    'min_order_num' => $val,
                    'max_order_num' => $post['max_order_num'][$k],
                    'award_amount' => $post['award_amount'][$k]
                ];
            }
        }
        if($post['x_goods_id']){
            foreach ($post['x_goods_id'] as $k => $val){
                $data['star']['goods'][] = [
                    'goods_id' => $val,
                    'goods_img' => $post['x_goods_img'][$k]
                ];
            }
        }
        if($post['name']){
            foreach ($post['name'] as $k => $val){
                $data['star']['config'][] = [
                    'name' => $val,
                    'level' => $post['level'][$k],
                    'zt_role_id' => $post['zt_role_id'][$k],
                    'team_role_id' => $post['team_role_id'][$k],
                    'zt' => $post['zt'][$k],
                    'team' => $post['team'][$k],
                    'brokerage' => $post['brokerage'][$k],
                    'brokerage_limit' => $post['brokerage_limit'][$k],
                ];
            }
        }
        $data['star'] = json_encode($data['star']);
        $data['team'] = json_encode($data['team']);
        $res = Db::name('lc_config')->where('id',1)->update($data);
        if ($res == false) return $this->error();
        return $this->success('设置成功.');
    }


    /*------------------------------------------------------ */
    //-- 佣金相关自动执行
    /*------------------------------------------------------ */
    public function autoEval()
    {
        Db::startTrans();//事务启用
        $res = (new DividendModel)->evalArrival();
        if ($res == false) {
            Db::rollback();//事务回滚
            return $this->error('执行失败-2，请重试.');
        }
        Db::commit();//事务提交
        return $this->success('操作成功.');
    }

    /*------------------------------------------------------ */
    //-- 上传分享海报背景图片
    /*------------------------------------------------------ */
    public function uploadShareBg(){
        $result = $this->_upload($_FILES['file'],'share_bg/');
        if ($result['error']) {
            return $this->error('上传失败，请重试.');
        }
        $file_url = str_replace('./','/',$result['info'][0]['savepath'].$result['info'][0]['savename']);
        $data['code'] = 1;
        $data['msg'] = "上传成功";
        $data['image'] = array('thumbnail'=>$file_url,'path'=>$file_url);
        return $this->ajaxReturn($data);
    }
    /*------------------------------------------------------ */
    //-- 删除图片
    /*------------------------------------------------------ */
    public function removeImg() {
        $file = input('post.url','','trim');
        unlink('.'.$file);
        return $this->success('删除成功.');
    }
    /*------------------------------------------------------ */
    //-- 海报合成处理
    /*------------------------------------------------------ */
    public function mergeShareImg(){

        $MergeImg = new \lib\MergeImg();

        $post = input('post.');
        $share_bg = $post['ShareBg']['path'][0];
        $share_bg = explode(',',$share_bg);
        $data['share_bg'] = $share_bg[0];
        if (empty($data['share_bg'])){
            return false;
        }

        $data['share_avatar'] = './a_static/share/avatar.jpg';
        $data['share_nick_name'] = '邀请者昵称';
        $data['share_qrcode'] = './a_static/share/qrcode.png';

        $data['share_avatar_xy'] = $post['setting']['share_avatar_xy'];
        $data['share_avatar_width'] = $post['setting']['share_avatar_width'];
        $data['share_avatar_shape'] = $post['setting']['share_avatar_shape'];
        $data['share_nick_name_xy'] = $post['setting']['share_nick_name_xy'];
        $data['share_nick_name_color'] = $post['setting']['share_nick_name_color'];
        $data['share_nick_name_center'] = $post['setting']['share_nick_name_center'];
        $data['share_nick_name_size'] = $post['setting']['share_nick_name_size'];
        $data['share_qrcode_xy'] = $post['setting']['share_qrcode_xy'];
        $data['share_qrcode_width'] = $post['setting']['share_qrcode_width'];

        $MergeImg->shareImg($data,'./upload/share_bg/test_share.jpg');
        return $this->success('请求成功.');
    }

    /*------------------------------------------------------ */
    //-- 设置股东分红池
    /*------------------------------------------------------ */
    public function selectGoods()
    {
        $type = input('type', 0, 'intval');
//        $page = input('page', 1, 'intval');
        $limit = input('limit', 10, 'intval');
        $goods_list = Db::name('shop_goods')->where(['is_delete' => 0, 'goods_type' => 4,'isputaway' => 1])->paginate(20);
        $this->assign("type", $type);
        $this->assign("goods_list", $goods_list);
        $this->assign('goodsType', [0 => '普通商品', 4 => '加盟商品', 5 => '免费领商品']);
        return $this->fetch('sys_admin/ladder/select_goods');
    }

}
